<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Services;

use App\Models\Config;

/**
 * 系统配置服务层 : 提供各模块所需配置信息、限制条件等服务
 *
 * Class ConfigService
 * @package App\Services
 */
class ConfigService
{
    /**
     * 获取所有配置信息
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable|mixed
     */
    public static function getAllList()
    {
        $result = CacheService::getCache('configServiceCache');
        if ($result == null) {
            $result = collect(Config::all()->toArray())->groupBy('module')->all();
            CacheService::setCache('configServiceCache', $result);
        }
        return $result;
    }

    /**
     * 获取指定模块配置
     * @param string $module 模块名
     * @return array|mixed
     */
    public static function getModuleConfig($module = "")
    {
        $getAllList = self::getAllList();
        if (!$module || empty($getAllList) || !isset($getAllList[$module])) return [];
        $result = [];
        collect($getAllList[$module])->each(function ($value, $index) use (&$result) {
            $result[$value['field']] = $value;
        });
        return $result;
    }

    /**
     * 根据模块、字段获取指定配置项
     * @param string $module
     * @param string $field
     * @return mixed|string
     */
    public static function getConfigItem($module = "", $field = "")
    {
        $getModuleConfig = self::getModuleConfig($module);
        if (empty($getModuleConfig) || !$field) return '';
        return $getModuleConfig[$field];
    }

    /**
     * 根据模块、字段获取指定配置项的值
     * @param string $module
     * @param string $field
     * @param false $isExplode 是否拆分成数组，前提：字符串必须是英文逗号隔开
     * @return false|mixed|string|string[]
     */
    public static function getConfigItemValue($module = "", $field = "", $isExplode = false)
    {
        $getConfigItem = self::getConfigItem($module, $field);
        if (empty($getConfigItem)) return "";
        return $isExplode ? explode(',', $getConfigItem['field_value']) : $getConfigItem['field_value'];
    }


    /**
     * 获取模块下“字段-值”配置组合列表
     * @param string $module
     * @return array
     */
    public static function getConfigItemValueList($module = "")
    {
        $getModuleConfig = self::getModuleConfig($module);
        if (empty($getModuleConfig)) return [];
        $result = [];
        collect($getModuleConfig)->each(function ($value, $key) use (&$result) {
            $result[$key] = $value['field_value'];
        });
        return $result;
    }

    /**
     * 获取站点配置
     * @return array
     */
    public static function getSiteConfig()
    {
        $siteConfig = self::getConfigItemValueList('site');
        if (isset($siteConfig['logo_image'])) {
            $logo_image = AttachmentService::previewList($siteConfig['logo_image']);
            $siteConfig['logo_image'] = $logo_image ? $logo_image[0]['url'] : '';
        }
        if (isset($siteConfig['qrcode_image'])) {
            $siteConfig['qrcode_image'] = AttachmentService::getReponsePicture($siteConfig['qrcode_image']);
        }
        return $siteConfig;
    }

    /**
     * 获取留言咨询配置
     * @return array
     */
    public static function getGuestbookConfig()
    {
        return self::getConfigItemValueList('guestbook');
    }

}
